﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace KZ400B
{
    public delegate void UserSettingChangeHandle();
    public class MySetting
    {
        public static event UserSettingChangeHandle OnUserSettingChange;
        ///<!--位置显示小数点显示位数-->
        const string stNamePositionDisplayDecimals = "PositionDisplayDecimals";
        ///<!--单步运行位移显示小数点显示位数-->
        const string stNameOSDisplayDecimals = "OSDisplayDecimals";

        

        ///<!--运行周期 应该大于 20 us-->
        const string stNameRunPWMLimit = "RunPWMLimit";
        ///<!--归零周期应该大于 14 us-->
        const string stNameToZeroPWMLimit = "ToZeroPWMLimit";

        static string s_dfPsDisDeciFormatter = "0.0000";
        static string s_stPsDisDeciFormatter = string.Empty;

        /// <summary>
        /// 联动各轴存储命名规则
        /// </summary>
        static string s_stUnionStoreFormatter = "{0}_UnionStore";
        /// <summary>
        /// 联动各轴选中状态存储命名规则
        /// </summary>
        static string s_stUnionCheckStoreFormatter = "{0}_UnionCheckStore";

        static int s_psDisDeciBits = 0;

        public static int PositionDisplayDecimals
        {
            get { return s_psDisDeciBits; }
            set { 
                s_psDisDeciBits = value;

                s_stPsDisDeciFormatter = s_psDisDeciBits == 0
                ? s_dfPsDisDeciFormatter
                : getDisDeciFormatter(s_psDisDeciBits, s_dfPsDisDeciFormatter)
                ; 
            }
        }

        static int s_osDisDeciBits = 0;

        public static int OSDisplayDecimals
        {
            get { return s_osDisDeciBits; }
            set 
            { 
                s_osDisDeciBits = value;
                s_stOSDisDeciFormatter = getDisDeciFormatter(s_osDisDeciBits, s_dfOSDisDeciFormatter); 
            }
        }

        /// <summary>
        /// 位置显示小数点显示位数
        /// </summary>
        public static string PositionDisDeciFormatter
        {
            get
            {
                if (string.IsNullOrEmpty(s_stPsDisDeciFormatter))
                {
                    s_stPsDisDeciFormatter = s_psDisDeciBits == 0 
                        ? s_dfPsDisDeciFormatter 
                        : getDisDeciFormatter(s_psDisDeciBits, s_dfPsDisDeciFormatter)
                        ; 
                }
                return s_stPsDisDeciFormatter;
            }
            
        }

        static string s_dfOSDisDeciFormatter = "0.0000000000";
        static string s_stOSDisDeciFormatter = string.Empty;

        /// <summary>
        /// 单步运行位移显示小数点显示位数
        /// </summary>
        public static string OneStepDisDeciFormatter
        {
            get
            {
                if (string.IsNullOrEmpty(s_stOSDisDeciFormatter))
                { 
                    s_stOSDisDeciFormatter = 
                    getDisDeciFormatter(s_osDisDeciBits, s_dfOSDisDeciFormatter); 
                }
                return s_stOSDisDeciFormatter;
            }

        }

        static string getDisDeciFormatter(int bits,string dfValue)
        {
            string sValue = string.Empty;
            try
            {
                
                if (bits > 0)
                {
                    StringBuilder s1 = new StringBuilder();
                    s1.Append("0.");
                    for (int i = 0; i < bits; i++)
                    {
                        s1.Append("0");
                    }
                    //if (!string.IsNullOrEmpty(sValue))
                    //{
                        sValue = s1.ToString();
                    //}
                }
                    
            }
            finally
            {
                if (string.IsNullOrEmpty(sValue))
                {
                    sValue = dfValue;
                }
            }
            return sValue;
        }

        #region 目前已经过时的代码
        /////
        /////<!--位置显示小数点显示位数-->
        /////
        //static string  getDisDeciFormatter(string keyName,string dfValue)
        //{
        //    string sValue = string.Empty;
        //    try
        //    {
        //        string tmp = ConfigurationManager.AppSettings[keyName];
        //        if (!string.IsNullOrEmpty(tmp))
        //        {
        //            int x1 = 0;
        //            if (int.TryParse(tmp, out x1))
        //            {
        //                if (x1 > 0)
        //                {
        //                    StringBuilder s1 = new StringBuilder();
        //                    s1.Append("0.");
        //                    for (int i = 0; i < x1; i++)
        //                    {
        //                        s1.Append("0");
        //                    }
        //                    if (!string.IsNullOrEmpty(sValue))
        //                    {
        //                        sValue = s1.ToString();
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    finally 
        //    {
        //        if (string.IsNullOrEmpty(sValue))
        //        {
        //            sValue = dfValue;
        //        }
        //    }
        //    return sValue;
        //}


        /////
        /////<!--位置显示小数点显示位数-->
        /////
        //static void getOSDisDeciFormatter()
        //{
        //    try
        //    {
        //        string tmp = ConfigurationManager.AppSettings[stNamePositionDisplayDecimals];
        //        if (!string.IsNullOrEmpty(tmp))
        //        {
        //            int x1 = 0;
        //            if (int.TryParse(tmp, out x1))
        //            {
        //                if (x1 > 0)
        //                {
        //                    StringBuilder s1 = new StringBuilder();
        //                    s1.Append("0.");
        //                    for (int i = 0; i < x1; i++)
        //                    {
        //                        s1.Append("0");
        //                    }
        //                    if (!string.IsNullOrEmpty(s_stPsDisDeciFormatter))
        //                    {
        //                        s_stPsDisDeciFormatter = s1.ToString();
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    finally
        //    {
        //        if (string.IsNullOrEmpty(s_stPsDisDeciFormatter))
        //        {
        //            s_stPsDisDeciFormatter = s_dfPsDisDeciFormatter;
        //        }
        //    }
        //}

        //static int getIntValue(string keyName,int defaultValue)
        //{
        //    int intValue = 0;
        //try
        //    {
        //        string tmp = ConfigurationManager.AppSettings[keyName];
        //        if (!string.IsNullOrEmpty(tmp))
        //        {
        //            int x1 = 0;
        //            if (int.TryParse(tmp, out x1))
        //            {
        //                intValue = x1;
        //            }
        //        }
        //    }
        //    catch(Exception ex)
        //    {
        //        intValue = defaultValue;
        //    }
        //    return intValue;
        //}
        #endregion


        static int s_dfRunPWMLimit = 20;
        static int s_stRunPWMLimit = 0;

        public static int RunPWMLimit
        {
            //get { if (s_stRunPWMLimit == 0) { s_stRunPWMLimit = getIntValue(stNameRunPWMLimit, s_dfRunPWMLimit); } return s_stRunPWMLimit; }
            get { return s_stRunPWMLimit == 0 ? s_dfRunPWMLimit : s_stRunPWMLimit; }
            set { s_stRunPWMLimit = value; }
        }

        static int s_dfToZeroPWMLimit = 14;
        static int s_stToZeroPWMLimit = 0;

        public static int ToZeroPWMLimit
        {
            //get { if (s_stToZeroPWMLimit == 0) { s_stToZeroPWMLimit = getIntValue(stNameToZeroPWMLimit, s_dfToZeroPWMLimit); } return s_stToZeroPWMLimit; }
            get { return s_stToZeroPWMLimit == 0 ? s_dfToZeroPWMLimit : s_stToZeroPWMLimit; }
            set { s_stToZeroPWMLimit = value; }
        }

        static string getUnionStoreAxleName(string axleName)
        {
            return string.Format(s_stUnionStoreFormatter,axleName.ToUpper());
        }

        static Dictionary<string, double> s_stUnionStoreAxleValue = new Dictionary<string, double>
        {
             {"X",0.0F}
            ,{"Y",0.0F}
            ,{"Z",0.0F}
            ,{"T",0.0F}
            ,{"U",0.0F}
            ,{"W",0.0F}
        };

        public static Dictionary<string, double> MutiAxleControlStoreValue
        {
            get { return MySetting.s_stUnionStoreAxleValue; }
            //set { MySetting.s_stUnionStoreAxleValue = value; }
        }

        public static double getUnionStoreAxleValue(string axleName)
        {
            string aName = axleName.ToUpper();
            if (s_stUnionStoreAxleValue.ContainsKey(aName))
            {
                return s_stUnionStoreAxleValue[aName];
            }
            return 0.0F;
        }

        public static void setUnionStoreAxleValue(string axleName, double aValue)
        {
            string aName = axleName.ToUpper();
            if (s_stUnionStoreAxleValue.ContainsKey(aName))
            {
                if (aValue != s_stUnionStoreAxleValue[aName])
                {
                    s_stUnionStoreAxleValue[aName] = aValue;
                    Save();
                }
            }
        }

        /// <summary>
        /// 联动各轴选中状态
        /// </summary>
        static Dictionary<string, bool> s_stUnionCheckStoreAxleValue = new Dictionary<string, bool>
        {
             {"X",false}
            ,{"Y",false}
            ,{"Z",false}
            ,{"T",false}
            ,{"U",false}
            ,{"W",false}
        };

        public static Dictionary<string, bool> MutiAxleControlCheckStoreValue
        {
            get { return MySetting.s_stUnionCheckStoreAxleValue; }
            //set { MySetting.s_stUnionStoreAxleValue = value; }
        }

        public static bool getUnionCheckStoreAxleValue(string axleName)
        {
            string aName = axleName.ToUpper();
            if (s_stUnionCheckStoreAxleValue.ContainsKey(aName))
            {
                return s_stUnionCheckStoreAxleValue[aName];
            }
            return false;
        }

        public static void setUnionCheckStoreAxleValue(string axleName, bool aValue)
        {
            string aName = axleName.ToUpper();
            if (s_stUnionCheckStoreAxleValue.ContainsKey(aName))
            {
                if (aValue != s_stUnionCheckStoreAxleValue[aName])
                {
                    s_stUnionCheckStoreAxleValue[aName] = aValue;
                    Save();
                }

            }
        }

        /// <summary>
        /// 虚拟零位设定
        /// </summary>
        /// <summary>
        /// 联动各轴选中状态
        /// </summary>
        static Dictionary<string, bool> s_stVZeroStoreAxleValue = new Dictionary<string, bool>
        {
             {"X",false}
            ,{"Y",false}
            ,{"Z",false}
            ,{"T",false}
            ,{"U",false}
            ,{"W",false}
        };

        public static Dictionary<string, bool> VZeroSetStoreValue
        {
            get { return MySetting.s_stVZeroStoreAxleValue; }
            //set { MySetting.s_stUnionStoreAxleValue = value; }
        }

        public static bool getVZeroSetStoreAxleValue(string axleName)
        {
            string aName = axleName.ToUpper();
            if (s_stVZeroStoreAxleValue.ContainsKey(aName))
            {
                return s_stVZeroStoreAxleValue[aName];
            }
            return false;
        }

        public static void setVZeroSetStoreAxleValue(string axleName, bool aValue)
        {
            string aName = axleName.ToUpper();
            if (s_stVZeroStoreAxleValue.ContainsKey(aName))
            {
                if (aValue != s_stVZeroStoreAxleValue[aName])
                {
                    s_stVZeroStoreAxleValue[aName] = aValue;
                    Save();
                }

            }
        }
        
        

        public static void Save()
        {
            if (OnUserSettingChange != null)
            {
                OnUserSettingChange();
            }
        }

    }
}
